package com.fc.service;

import com.fc.dao.UserDao;
import com.fc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

/**
 * User 业务逻辑类实现
 */

@Service("userBiz")
public class UserBizImpl implements UserBiz {

	@Autowired
	@Qualifier("userDao")
	private UserDao userDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public User login(String name,String password) {
		User user = null;
		try {

			List<User> users = userDao.findUserByProperty("name", name);
			Iterator<User> it = users.iterator();
			if (it.hasNext()) {
				user = (User) it.next();
				if (password.equals(user.getPassword())) return user;
			}
			return null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		}

	}

	@Override
	public void save(User user) {
		userDao.save(user);
		int n = 1/0;
	}

	public List<User> findEmpsByPage(int pageNo, int pageSize) {

		List<User> users = null;

		users = userDao.findByPage((pageNo - 1) * pageSize, pageSize);

		return users;
	}

}
